/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package name.coolwind.cxblog.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import name.coolwind.cxblog.domain.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

/**
 *
 * @author coolwind
 */
@Repository
public class TagDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void createTag(Tag tag) {
        String sqlStr = "insert into cx_tag(tag_name) values(?)";
        this.jdbcTemplate.update(sqlStr, tag.getTagName());
    }

    public void updateTag(Tag tag) {
        String sqlStr = "update cx_tag set tag_name = ? where tag_id = ?";
        this.jdbcTemplate.update(sqlStr, tag.getTagName(), tag.getTagId());
    }

    public void deleteTag(Tag tag) {
        String sqlStr = "delete from cx_tag where tag_id = ?";
        this.jdbcTemplate.update(sqlStr, tag.getTagId());
    }

    /**
     *
     * @param pageIndex
     * @param perPage
     * @return
     * if perPage equals zero, the function will return all tags, else return a page tags
     */
    public List<Tag> getTagsByPage(int pageIndex, int perPage) {
        final List<Tag> tagList = new LinkedList();
        if (perPage == 0) {
            String sqlStr = "select * from cx_tag";
            jdbcTemplate.query(sqlStr, new RowCallbackHandler() {

                @Override
                public void processRow(ResultSet rs) throws SQLException {
                    Tag tag = new Tag();
                    tag.setTagId(rs.getInt("tag_id"));
                    tag.setTagName(rs.getString("tag_name"));
                    tagList.add(tag);
                }

            });
        }else{
            String sqlStr = "select * from cx_tag limit ?,?";
            jdbcTemplate.query(sqlStr, new Object[]{pageIndex, perPage}, new RowCallbackHandler() {

                @Override
                public void processRow(ResultSet rs) throws SQLException {
                    Tag tag = new Tag();
                    tag.setTagId(rs.getInt("tag_id"));
                    tag.setTagName(rs.getString("tag_name"));
                    tagList.add(tag);
                }
            });
        }

        return tagList;
    }
    
    public String getTagNameByTagId(String tagId){
        String sqlStr = "select tag_name from cx_tag where tag_id = ?";
        final Tag tag = new Tag();
        this.jdbcTemplate.query(sqlStr, new Object []{tagId}, new RowCallbackHandler(){

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                tag.setTagName(rs.getString("tag_name"));
            }
            
        });
        
        return tag.getTagName();
    }

}
